#! /usr/bin/env python
#coding=utf-8
from uliweb.orm import *
from uliweb.utils.common import get_var
import datetime
from sqlalchemy.sql import not_

def get_modified_user():
    from uliweb import request
    
    return request.user.id

def get_values(model, ids, condition=None):
    from uliweb.orm import get_model
    model = get_model(model)
    return model.all().filter(condition).values(*ids)


class Coordination(Model):
    
    requirement = Reference('requirement', verbose_name="准开发需求")
    type = Field(CHAR, max_length=1, verbose_name="类型", choices=get_var('PARA/COORDINATION_TYPE'))
    title = Field(str, verbose_name='协同单名称', max_length=255, required=True)
    sender = Reference('kfzx', verbose_name='提出单位', collection_name='sender_coordinations')
    receivers = manytomany('kfzx', verbose_name='处理单位', collection_name='receiver_coordinations')
    description = Field(TEXT, verbose_name='描述')
    opinion = Field(TEXT, verbose_name='主管条线意见')
    status = Field(CHAR, verbose_name='协同单状态', max_length=1, required=True, choices=get_var('PARA/ASSE_STATUS'))
    creator = Reference('user', verbose_name='创建人', collection_name='creator_coordinations')
    modified_user = Reference('user', verbose_name='修改人', default=get_modified_user, auto=True, auto_add=True)
    created_date = Field(datetime.date, verbose_name='创建时间', auto_now_add=True)
    modified_date = Field(datetime.datetime, verbose_name='修改时间', auto_now=True, auto_now_add=True)

    def __unicode__(self):
        return self.title
    
    def __str__(self):
        return self.title
    
 
class CoordinationProcess(Model):
    
    coordination = Reference('coordination', verbose_name="协同单")
    kfzx = Reference('kfzx', verbose_name='评估单位')
    opinion = Field(TEXT, verbose_name='评估意见')
    ordering = Field(int,verbose_name='顺序号')
    created_date = Field(datetime.date, verbose_name='开始时间', auto_now_add=True)
    finish_date = Field(datetime.date, verbose_name='要求完成时间')
    status = Field(CHAR, verbose_name='提交状态', max_length=1, required=True, choices=get_var('PARA/ASSE_STATUS'))
    sender = Reference('user', verbose_name='上一站处理人')
    receiver_kfzx = manytomany('kfzx', verbose_name='下一站处理中心', collection_name='assessment_receiver_kfzx')
    receiver = manytomany('user', verbose_name='下一站处理人', collection_name='assessment_receiver_user')
    modified_user = Reference('user', verbose_name='评估人', default=get_modified_user, auto=True, auto_add=True)
    modified_date = Field(datetime.datetime, verbose_name='修改时间', auto_now=True, auto_now_add=True)
    attachment = manytomany('assessementattachment', verbose_name='附件', collection_name='coordination_attachment')
    